package com.kaifamiao.demo.d0902;


import java.sql.*;

public class JDBCTest {

    public static void main(String[] args) {
        try {
            // mysql 提供的驱动， Driver 的实现类
//            Driver driver = new com.mysql.cj.jdbc.Driver();
//            // 注册驱动
//            DriverManager.registerDriver(driver);

            // 加载 Driver 类, 注册驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 获取数据库连接
            String url = "jdbc:mysql://localhost:3306/kfm";
            String user = "root";
            String password = "";
            Connection connection = DriverManager.getConnection(url, user, password);


            // 创建 statement 对象
            Statement statement = connection.createStatement();
            // 执行SQL
            String sql = """
                    insert into users (username, password) VALUES ("wanger", '112233');
                    """;
//            boolean result = statement.execute(sql);
            int result = statement.executeUpdate(sql, Statement.RETURN_GENERATED_KEYS);

            ResultSet generatedKeys = statement.getGeneratedKeys();
            while (generatedKeys.next()){
                int id = generatedKeys.getInt(1);
                System.out.println(id);
            }
            // 关闭连接
            statement.close();
            connection.close();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
